 $(document).ready(function() {
             //		权限判断
		if($.cookie('ID') == null){
			window.location.href="http://www.duoweish.com/crm/login.html";
		}
//		2，3号权限
		if($.cookie('Right') == 1){
			window.location.href="http://www.duoweish.com/crm/index.html";
		}
 				
			 var page_num = 0;	
			 var list_peo;
			 //任命人列表
			 $('.dw_name').text($.cookie('Name'));
		 	$('.dw_part').text($.cookie('Department'));
			 $.ajax({
					type:"get",
					url:"http://www.duoweish.com/apis/NewProjectInfo/",
					async:true,
					success:function(data){
						list_peo = data;
					}
				});
			 
             $(".select2").select2();
           $('#reservation').daterangepicker({
             	format:'YYYY-MM-DD',
             	startDate:'2017-10-10',
             	locale:{
             		format:'YYYY-MM-DD'
             	}
             });
             
             
             init(1);
             
             $('.search_btn').click(function(){
             	 
			  Ajax(1)
				
            })
            function Ajax(curr){
            	 var Kind_data = 0;
			      var onoff = "True";//客户名or项目名
			      var sea_text =$.trim($('.search_text').val());//搜索框内容
				  var data_rang = $('.time_range').val();//时间范围
				  
				  //时间范围转化
				  var start = data_rang.split(' -')[0];
				  var end = data_rang.split('- ')[1];
				if(data_rang==""){onoff="False";}
				if(curr==1){
					onoff = "False";
				}
            	$.ajax({
				headers:{
				    	Accept:"application/json;charset=utf-8"
				    },
					type:"get",
					url:"http://www.duoweish.com/apis/ProjectEdit/",
					data:{
                            UserId:$.cookie('ID'),
							Date:onoff,
							DateBegin:start,
							DateEnd:end,
							Content:sea_text,
							page:curr||1,
							rows:8
					},
					success:function(data){
						if(data.length!=0){
							page_num = data[1];
						    tableData(data);
					     	divePage(page_num,curr);
						}else{
							layer.msg('没找到该商家')
						}
						
						}
				})
            	
            }
            function tableData(ret){
            	var option_list=[];
                    
                    $('#table_body').empty();
                    for(var i = 0;i < ret.length;i++){
                    	for (var j = 0;j<ret[i].length;j++) {
                    		for (var n = 0;n<list_peo[0].length;n++) {
                    			                     var option_text;
                    	                            if(list_peo[0][n]==ret[i][j][1]){
                    	                            	option_text = '<option selected="selected">'+list_peo[0][n]+'</option>';
                    	                            }else{
                    	                            	 option_text = '<option>'+list_peo[0][n]+'</option>';
                    	                            }
													option_list +=  option_text;
												}
                    	
                        var tr = `<tr>
                                <td style="line-height:20px;"><input type="text" value="${ret[i][j][0]}" /></td>
                                <td style="line-height:20px;">
                                  <select>
                                  ${option_list}
                                  </select>
                                </td>
                                <td style="line-height:20px;"><input type="text" value="${ret[i][j][2]}" class="datepicker"/></td>
                                <td style="line-height:20px;"><input type="text" value="${ret[i][j][3]}" /></td>
                                <td index="${ret[i][j][4]}">
                                   <button  class="look_ok">查看</button>
                                </td>
                               <td index="${ret[i][j][4]}">
                                    <button class="edit_btn check1" index="1"><i style="font-size: 20px;" class="fa fa-pencil"></i></button>
                                	<span style="display:inline-block;width:20px;"></span>
                                	<button class="edit_btn check2" index="0"><i style="font-size: 20px;" class="fa fa-trash-o"></i></button>
                                
                                </td>
                            </tr>`;
                        $('#table_body').append(tr);
                        
                    }
                 $('.datepicker').datepicker({
                 	autoclose: true, 
                 	startDate:'2018-8-8',
                 	format: 'yyyy-mm-dd',
                 	language: 'cn'
                 })
                }
                   
                   
                 $('.look_ok').click(function(){
                 	var index = $(this).parent().attr('index');
                 	    layer.open({
								        type: 2
								        ,title: "产品详情"
								        ,anim:1
								        ,closeBtn: 1
								        ,area:['640px','600px']
								        ,shade: 0.8
								        ,id: 'LAY_layuipro' 
								        ,shadeClose:true
								        ,btnAlign: 'c'
								        ,moveType: 1 
								        ,content:'http://www.duoweish.com/crm/item_detail.html?id_data='+index
									    ,success: function(){
									    	
						               }
					    			  
							    })
                 })
                   
                 $('.check1').click(function(){
                 	        var index = $(this).parent().attr("index");
							var item_info=[];
							var Parent = $(this).parent().parent().find("td");
							item_info[0] = Parent.eq(0).find("input").val();
							item_info[1] = Parent.eq(1).find("select").find("option:selected").text();
							item_info[2] = Parent.eq(2).find("input").val();
							item_info[3] = Parent.eq(3).find("input").val();
							$.ajax({
								type:"get",
								url:"http://www.duoweish.com/apis/ProjectEditSubmit/",
								async:true,
								data:{
									 Id:index,
									 Name:item_info[0],
									 Manage:item_info[1],
									 Time:item_info[2],
									 Warning:item_info[3],
								},
								success:function(){
									layer.msg('修改成功');
									location.reload();
								}
							});
                 })
                  $('.check2').click(function(){
                 	        var index = $(this).parent().attr("index");
                 	        var self = $(this);
							$.ajax({
								type:"get",
								url:"http://www.duoweish.com/apis/ProjectDel/",
								async:true,
								data:{
									Project:index
								},
								success:function(data){
									if(data=='success'){
										layer.msg('删除成功',{time:500},function(){
											self.parents("tr").remove();
										});
									}
									
								}
							});
                 })
      
                    
            }
          
           function divePage(page_num,curr){
            layui.use(['laypage', 'layer'], function(){
					 var laypage = layui.laypage
			  		,layer = layui.layer;
			  	
                     laypage.render({
						    elem: 'demo0'
						    ,count: page_num
						    ,limit:8
						    ,curr:curr
						    ,jump: function(obj, first){
						    	   if(!first){
						                Ajax(obj.curr);
						               }
				        
						      		}
						   		});
					 })		
           }
          
           function init(curr){ 
           	     Ajax(1)
	            divePage(page_num);
           }
          
     
           
})